package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EnterstorageFormMap;
import com.iemes.entity.LabelprintFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.warehouse.EnterstorageMapper;
import com.iemes.mapper.warehouse.LabelprintMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/label_return/")
@Controller
public class Label_ReturnController extends BaseController {

	@Inject
	private EnterstorageMapper enterstorageMapper;
	
	@Inject
	private LabelprintMapper labelprintMapper;
	
	// 查看退回标签表，显示按钮。
	@RequestMapping("label_return")
	public String label_damage(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/label_return_list";
	}

	// 退回标签表分页
	@ResponseBody
	@RequestMapping("label_return_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		LabelprintFormMap labelprintFormMap = getFormMap(LabelprintFormMap.class);
		List<Map<String, Object>> enter=labelprintMapper.getAllPrint(labelprintFormMap);
		LuogicPaging(pageNow, pageSize, enter);
		pageView.setRecords(enter);
		return pageView;
	}
	
	// 编辑退回标签表
	@RequestMapping("label_return_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询退回标签入库报表
			LabelprintFormMap label=labelprintMapper.getById(id);
			model.addAttribute("label",label);
		}
		return Common.BACKGROUND_PATH + "/warehouse/label_return_edit";
	}
	
	// 修改退回标签表
	@ResponseBody
	@RequestMapping(value = "label_return_editEntity")
	@SystemLog(module = "编辑退回标签", methods = "打印标签-修改退回标签") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws NumberFormatException, Exception {
		LabelprintFormMap labelprintFormMap=getFormMap(LabelprintFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		labelprintFormMap.put("formCategory", 1);
		labelprintFormMap.put("returnDate", sdf.format(date));
		labelprintFormMap.put("rFillPeopleId", userFormMap.getInt("id"));
		baseMapper.editEntity(labelprintFormMap);
		// 获取对应的入库报表Id
		int enterId=Integer.parseInt(request.getParameter("labelprintFormMap.enterId"));
		//计算“标签退回总数”
		int returnTotal=0;
		//计算“标签损坏总数”
		int damageTotal=0;
		List<LabelprintFormMap> labels=labelprintMapper.getByEnterId(enterId);
		for(LabelprintFormMap l:labels){
			String returnCount=l.getStr("returnCount");
			String damageCount=l.getStr("damageCount");
			if(returnCount!=null){
				returnTotal+=Integer.parseInt(returnCount);
			}
			if(damageCount!=null){
				damageTotal+=Integer.parseInt(damageCount);
			}
		}
		//计算“标签打印总数”
		EnterstorageFormMap enterstorageFormMap=enterstorageMapper.getEnterstorangByid(enterId);
		int printTotal=enterstorageFormMap.getInt("printTotal");
		enterstorageFormMap.put("abolishTotal", returnTotal+damageTotal);
		enterstorageFormMap.put("balance", printTotal-returnTotal-damageTotal);
		enterstorageMapper.editEntity(enterstorageFormMap);
		return "success";
	}
}
